package com.lizhenhua.api.service.impl;

import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lizhenhua.api.feignClient.OrderItemAddFeignClient;
import com.lizhenhua.api.feignClient.OrderSaveFeignClient;
import com.lizhenhua.api.feignClient.StockDelFeignClient;
import com.lizhenhua.api.feignClient.StockUpdateFeignClient;
import com.lizhenhua.api.service.OrderSubmitService;
import com.lizhenhua.fmmall.beans.Orders;
import com.lizhenhua.fmmall.beans.ProductSku;
import com.lizhenhua.fmmall.beans.ShoppingCartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class OrderSubmitServiceImpl implements OrderSubmitService {


    @Autowired
    private StockUpdateFeignClient stockUpdateFeignClient;


    @Autowired
    private OrderSaveFeignClient orderSaveFeignClient;


    @Autowired
    private StockDelFeignClient stockDelFeignClient;
    @Autowired
    private OrderItemAddFeignClient orderItemAddFeignClient;

    @Autowired
    private ObjectMapper o;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    /**
     * 如果上层 已经有了  事务的定义  下面调用方法  如果也是一个  事务 那么 他们会作为参与者 添加到当前的事务发起者  也就是添加到当前的事务组
     *
     * @return
     * @Transactional // 和下面Lcn 一起用
     * @LcnTransaction
     */

    @Override
    @Transactional    // 和下面Lcn 一起用
    @LcnTransaction  // 分布式事务注解   因为他是通过代理模式管理事务的  所以在上面要加 T  @Transactional  注解
    public Map<String, String> addOrder(String cids, Orders orders) {

        // 1. 保存订单
        // 生成订单编号
        String orderId = UUID.randomUUID().toString().replace("-", "");
        orders.setOrderId(orderId);
        // 分布式事务  需要在此方法上加入    @Transactional      @LcnTransaction  注解
        List<ShoppingCartVo> shoppingCartVos = orderSaveFeignClient.saveOrder(cids, orders);
        // 封装返回内容
        Map<String, String> map = null;


        // 2. 保存订单快照
        if (shoppingCartVos != null) {
            int i = orderItemAddFeignClient.addOrderItem(shoppingCartVos, orderId);
            System.out.println(i);
            if (i > 0) {
                List<ProductSku> skus = new ArrayList<>();
                for (ShoppingCartVo s : shoppingCartVos
                ) {
                    String skuId = s.getSkuId();
                    int newStock = s.getSkuStock() - Integer.parseInt(s.getCartNum());
                    // 根据主键修改 属性值
                    ProductSku productSku = new ProductSku();
                    productSku.setSkuId(skuId);
                    productSku.setStock(newStock);
                    skus.add(productSku);
                }
                int update = stockUpdateFeignClient.update(skus);
                System.out.println(update);
                if (update > 0) {

                    // 4. 删除购物车
                    int k = stockDelFeignClient.DelShop(cids);

                    if (k > 0) {
                        String untitled = "";

                        for (ShoppingCartVo s : shoppingCartVos
                        ) {
                            untitled += s.getSkuName() + ",";
                        }
                        // 去掉 ,号
                        untitled = untitled.substring(0, untitled.length() - 2);

                        map = new HashMap<>();
                        map.put("orderId", orderId);
                        map.put("productNames", untitled);
                    }

                }


            }


        }

        return map;
    }
}
